
import { listAppByPageUsingPost } from '@/services/backend/appController';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { Space, Typography } from 'antd';
import React, { useRef, useState } from 'react';
import ReviewModal from './components/ReviewModal';


/**
 * 审核管理页面
 *
 * @constructor
 */
const ReviewPage: React.FC = () => {
    // 是否显示新建窗口
    const [reviewModalVisible, setReviewModalVisible] = useState<boolean>(false);
    const actionRef = useRef<ActionType>();
    // 当前用户点击的数据
    const [currentRow, setCurrentRow] = useState<API.App>();

    const handleReview = (row: API.App) => {
        setCurrentRow(row);
        setReviewModalVisible(true);
    }

    /**
     * 表格列配置
     */
    const columns: ProColumns<API.App>[] = [
        {
            title: 'id',
            dataIndex: 'id',
            valueType: 'text',
            hideInForm: true,
        },
        {
            title: '应用名称',
            dataIndex: 'appName',
            valueType: 'text',
        },
        {
            title: '应用图标',
            dataIndex: 'appIcon',
            valueType: 'image',
            fieldProps: {
                width: 64,
            },
            hideInSearch: true,
        },
        {
            title: '审核状态',
            dataIndex: 'reviewStatus',
            valueEnum: {
                0: {
                    text: '待审核',
                },
                1: {
                    text: '审核通过',
                },
                2: {
                    text: '审核拒绝',
                },
            },
            hideInForm: true,
        },
        {
            title: '应用描述',
            dataIndex: 'appDesc',
            valueType: 'textarea',
        },
        {
            title: '应用类型',
            dataIndex: 'appType',
            valueEnum: {
                0: {
                    text: '得分类',
                },
                1: {
                    text: '测评类',
                },
            },
        },
        {
            title: '评分策略',
            dataIndex: 'scoringStrategy',
            valueEnum: {
                0: {
                    text: '自定义',
                },
                1: {
                    text: 'AI',
                },
            },
        },
        {
            title: '创建人',
            dataIndex: 'userId',
            valueType: 'text',
            hideInForm: true
        },
        
        {
            title: '审核信息',
            dataIndex: 'reviewMessage',
            valueType: 'text',
            hideInForm: true,
        },
        {
            title: '审核人',
            dataIndex: 'reviewerId',
            valueType: 'text',
            hideInForm: true,
        },
        {
            title: '审核时间',
            dataIndex: 'reviewTime',
            valueType: 'dateTime',
            hideInForm: true,
        },
        {
            title: '创建时间',
            sorter: true,
            dataIndex: 'createTime',
            valueType: 'dateTime',
            hideInSearch: true,
            hideInForm: true,
        },
        {
            title: '更新时间',
            sorter: true,
            dataIndex: 'updateTime',
            valueType: 'dateTime',
            hideInSearch: true,
            hideInForm: true,
        },
        {
            title: '操作',
            dataIndex: 'option',
            valueType: 'option',
            render: (_, record) => (
                record.reviewStatus === 0 &&
                <Space size="middle">
                    <Typography.Link onClick={() => handleReview(record)}>
                        审核
                    </Typography.Link>
                </Space>
            ),
        },
    ];
    return (
        <PageContainer>
            <ProTable<API.App>
                headerTitle={'审核管理'}
                actionRef={actionRef}
                rowKey="id"
                search={{
                    labelWidth: 120,
                }}
                request={async (params, sort, filter) => {
                    const sortField = Object.keys(sort)?.[0];
                    const sortOrder = sort?.[sortField] ?? undefined;

                    const { data, code } = await listAppByPageUsingPost({
                        ...params,
                        sortField,
                        sortOrder,
                        ...filter
                    } as API.AppQueryRequest);

                    return {
                        success: code === 0,
                        data: data?.records || [],
                        total: Number(data?.total) || 0,
                    };
                }}
                columns={columns}
            />
            <ReviewModal
                visible={reviewModalVisible}
                oldData={currentRow}
                onSubmit={() => {
                    setReviewModalVisible(false);
                    actionRef.current?.reload();
                }}
                onCancel={() => {
                    setReviewModalVisible(false);
                }}
            />
        </PageContainer>
    );
};
export default ReviewPage;
